package com.vitabuybuy.tv.ui.mine.coin

import android.os.Bundle
import android.view.View
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityGroupBuyDetailBinding
import com.vitabuybuy.tv.databinding.ActivityUseRecordBinding
import com.vitabuybuy.tv.ui.mine.coin.viewmodel.RewardCoinViewModel
import kotlinx.android.synthetic.main.activity_use_record.*

class UseRecordActivity : BaseActivity<RewardCoinViewModel, ActivityUseRecordBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_use_record

    private var pageNumber = 1

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.use_record))
        mBinding.mViewModel = viewModel
    }

    override fun observeData() {
        viewModel.getRecordList(pageNumber)
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getRecordList(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getRecordList(pageNumber)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getRecordList(pageNumber)
            }
            1->{
                refresh.onLoad(msg.arg1)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, UseRecordActivity::class.java)
        }
    }
}
